﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;
using MCMLXVII.Core.Common;
using MCMLXVII.Core.Common.BackEnd;
using MCMLXVII.BackEnd.Data.SystemDatabase.DataSets;
using MCMLXVII.BackEnd.Services.Common.Messages;
using System.ServiceModel;

#endregion

namespace MCMLXVII.BackEnd.Data.SystemDatabase.Parameters
{

    public class ParametersCache
    {
        private static Hashtable _Cache=new Hashtable();
        private static bool Initialized = false;

        public ParametersCache()
        {
            Console.WriteLine(  );
        }
        #region GetVarchar
        public static string GetVarchar(string name)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))                
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"ParameterNotFound"));
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParametersVarchar))
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            return sp.ValueVarChar;
        }

        public static string GetVarchar(string name, string Defaultvalue)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))
                return Defaultvalue;
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParametersVarchar))
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            
            return sp.ValueVarChar;
        }
        #endregion

        #region GetInt
        public static int GetInt(string name)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))                
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"ParameterNotFound"));
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParameterInt ))
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            return sp.ValueInt;
        }

        public static int GetInt(string name, int Defaultvalue)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))
                return Defaultvalue;
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParameterInt ))
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            return sp.ValueInt;
        }
        #endregion

        #region GetDateTime
        public static DateTime GetDateTime(string name)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))                
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"ParameterNotFound"));
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParameterDatetime ))                
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            return sp.ValueDateTime;
        }

        public static DateTime  GetVarchar(string name, DateTime  Defaultvalue)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))
                return Defaultvalue;
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParameterDatetime))
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            return sp.ValueDateTime;
        }
        #endregion


        #region GetXML
        public static XmlDocument  GetXML(string name)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))                
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"ParameterNotFound"));
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParameterXML ))    
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            return sp.ValueXML;
        }

        public static XmlDocument  GetVarchar(string name, XmlDocument  Defaultvalue)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))
                return Defaultvalue;
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParameterXML))
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            return sp.ValueXML;
        }
        #endregion

        #region GetFloat
        public static float GetFloat(string name)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))                
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"ParameterNotFound"));
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParameterFloat ))                
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            return sp.ValueFloat;
        }

        public static float GetFloat(string name, float Defaultvalue)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(name))
                return Defaultvalue;
            SystemParameter sp = (SystemParameter)_Cache[name];
            if (!(sp.Type == SystemParameterType.ParameterFloat))
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"WrongParameterType"));
            
            return sp.ValueFloat;
        }
        #endregion

        public static SystemParameter[] GetParameters()
        {             
            DSParameters.ParametersDataTable tbl = new DSParameters.ParametersDataTable();
            tbl.Load(SqlHelper.ExecuteReader( CommandType.Text, "Select ParameterName, ParameterType, ValueVarchar, ValueDateTime, ValueInt, ValueXML, ValueFloat from Parameters"));
            SystemParameter[] res = new SystemParameter[tbl.Rows.Count];
            int i = 0;
            foreach(DSParameters.ParametersRow row in tbl.Rows)
            {
                SystemParameter SysPar;
                string Name = row.ParameterName;
                switch (row.ParameterType)
                {
                    case 1: SysPar = new SystemParameter(Name, row.ValueVarchar );
                        break;
                    case 2: SysPar = new SystemParameter(Name, row.ValueDateTime );
                        break;
                    case 3: SysPar = new SystemParameter(Name, row.ValueInt );
                        break;
                    case 4: XmlDocument xdoc = new XmlDocument();
                        xdoc.LoadXml(row.ValueXML );
                        SysPar = new SystemParameter(Name, xdoc);
                        break;
                    case 5: SysPar = new SystemParameter(Name, (float)row.ValueFloat );
                        break;
                    default:
                        throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Technical,Properties.Resources.InvalidParameterType));
                }
                res[i] = SysPar;
                i++;
            }
            return res;
        }

        public static SystemParameter GetParameter(string Name)
        {
            if (!Initialized)
            {
                InitializeCache();
            }
            if (!_Cache.Contains(Name))                           
                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Functional,"Parameter not found " + Name));

            return (SystemParameter)_Cache[Name];
        }

        private static void InitializeCache()
        {
            lock (_Cache)
            {                
                DSParameters.ParametersDataTable tbl = new DSParameters.ParametersDataTable();
                tbl.Load(SqlHelper.ExecuteReader( CommandType.Text, "Select ParameterName, ParameterType, ValueVarchar, ValueDateTime, ValueInt, ValueXML, ValueFloat from Parameters"));
                foreach(DSParameters.ParametersRow row in tbl.Rows)
                    try
                    {
                        SystemParameter SysPar;
                        string Name = row.ParameterName;
                        switch (row.ParameterType)
                        {
                            case 1: SysPar = new SystemParameter(Name, row.ValueVarchar );
                                break;
                            case 2: SysPar = new SystemParameter(Name, row.ValueDateTime );
                                break;
                            case 3: SysPar = new SystemParameter(Name, row.ValueInt );
                                break;
                            case 4: XmlDocument xdoc = new XmlDocument();
                                xdoc.LoadXml(row.ValueXML );
                                SysPar = new SystemParameter(Name, xdoc);
                                break;
                            case 5: SysPar = new SystemParameter(Name, (float)row.ValueFloat );
                                break;
                            default:
                                throw new FaultException<BackendFault>(new BackendFault(BackendFaulType.Technical,Properties.Resources.InvalidParameterType));
                        }
                        _Cache.Add(Name,  SysPar);
                    }
                    catch (Exception ex)
                    {
                        Tracer.Trace(ResourcesTools.RetornaLocPar(Properties.Resources.LoadParameterException, ex.Message), TraceLevel.Exceptions);
                    }
                }
                Initialized = true;
            }

        }
    }
